<?php

/**
 * This is the model class for table "subject".
 *
 * The followings are the available columns in table 'subject':
 * @property string $id
 * @property string $cnname
 * @property string $enname
 * @property string $code
 * @property integer $create_time
 * @property integer $status
 */
class Subject extends CActiveRecord {

    const STATUS_PENDING = 1;
    const STATUS_OK = 2;
    const STATUS_INVALID = 3;

    /**
     * Returns the static model of the specified AR class.
     * @return subject the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'subject';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('cnname', 'required'),
            array('cnname, enname, code', 'length', 'max' => 128),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, cnname, enname, code, create_time, status', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'Id',
            'cnname' => 'Cnname',
            'enname' => 'Enname',
            'code' => 'Code',
            'create_time' => 'Create Time',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);

        $criteria->compare('cnname', $this->cnname, true);

        $criteria->compare('enname', $this->enname, true);

        $criteria->compare('code', $this->code, true);

        $criteria->compare('create_time', $this->create_time);

        return new CActiveDataProvider('subject', array(
                    'criteria' => $criteria,
                ));
    }

    protected function beforeSave() {
        $this->create_time = time();
        $this->status = self::STATUS_PENDING;
        return parent::beforeSave();
    }

    public function deleteSubjectById($id) {
        self::model()->updateByPk($id, array('status' => self::STATUS_INVALID));
    }

}